require('./player.less')

//引入axios
const axios = require('axios')
require('../../utlis/axios')

//引入dom.js
const { $, g, ga } = require('../../utlis/dom')

//引入字体图标
require('../../libs/fonts/iconfont.css')


$(function () {

    let fragments = JSON.parse(localStorage.getItem('fragments'))
    console.log(fragments)
    /* 
        自动播放
    */
    let index = 0

    //总时间
    let alltime = 0

    //点进页面时间
    let startTime = Date.now()

    function thePlay() {
        g('#title').textContent = fragments[index].title
        g('#section').textContent = fragments.length
        g('#sec').textContent = index + 1
        video.src = 'http://47.96.154.185:3701' + fragments[index].videoUrl
        video.play()
    }
    thePlay()


    //自动播放下一曲
    video.addEventListener('ended', function () {
        index++
        if (index == fragments.length) index = 0
        thePlay()
    })

    //点击下一曲播放
    next.addEventListener('click', function () {
        index++
        if (index == fragments.length) index = 0
        thePlay()
    })

    //点击上一曲播放
    last.addEventListener('click', function () {
        index--
        if (index < 0) return
        thePlay()
    })

    //进度条
    video.addEventListener('timeupdate', function () {
        progress.style.width = this.currentTime / this.duration * 100 + '%'
    })


    //点击暂停按钮显示mask
    end.addEventListener('click', function () {
        imgs.src = 'http://47.96.154.185:3701' + fragments[index].imgUrl
        current.textContent = fragments[index].title
        video.pause()
        g('.mask').style.display = 'block'
        //暂停时间
        alltime += Date.now() - startTime
    })
    //点击继续按钮暂停视频
    continues.addEventListener('click', function () {
        video.play()
        g('.mask').style.display = 'none'

        //继续运动时间
        startTime = Date.now()
    })


    //点击结束按钮跳转
    stops.addEventListener('click', function () {
        const params = {
            type: 0,
            duration: Math.ceil(alltime / 100),
            calorie: Math.ceil(alltime / 100) * 1,
            meter: Math.ceil(alltime / 100) * 2
        }
        //console.log(params)

        axios.post('http://47.96.154.185:3701/api/exercise/save', params).then(function (res) {
            if (res.data.errno == 0) {
                location.href = './curriculum.html'
            }
        })
    })


})